package com.spider.im.common.config;

import com.spider.im.common.utils.EncryptionUtils;
import jakarta.annotation.Resource;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RedissonConfig {

    @Value("${spring.redis.address}")
    private String redisAddress;

    @Value("${spring.redis.pwd}")
    private String password;

    @Value("${spring.redis.user}")
    private String username;

    @Resource
    private EncryptionUtils encryptionUtils;

    @Bean(destroyMethod = "shutdown")
    public RedissonClient redisson() {
        String decryptPwd = encryptionUtils.decrypt(password);
        Config config = new Config();
        config.useSingleServer()
              .setAddress(redisAddress)
              .setPassword(decryptPwd)
              .setUsername(username)
              .setConnectionPoolSize(64)
              .setTimeout(3000)
              .setIdleConnectionTimeout(10000)
              .setConnectTimeout(10000);

        return Redisson.create(config);
    }
}